<?php


namespace App\Http\Controllers\Api;
use App\Http\Controllers\BaseController;
use App\Lib\Express;
use App\Lib\Kuaidi100;
use App\Models\ExpressType;
use App\Models\MallOrder;
use App\Models\MallOrderProduct;
use App\Services\MallOrderService;
use App\Services\MallRefundService;
use Illuminate\Http\Request;

class ExpressController extends BaseController
{



    public function index(Request $request) {
        $order_id = $request->input('order_id');
        $group_list = MallOrderProduct::where('order_id', $order_id)->where('express_order_sn', '!=', '')
            ->select('express_order_sn')->groupBy('express_order_sn')->get();

        $list = [];
        foreach ($group_list as $key => $item) {
            $temp['product_total'] = MallOrderProduct::where('order_id', $order_id)
                ->where('express_order_sn', $item->express_order_sn)
                ->count();

            $order_product_list = MallOrderProduct::where('order_id', $order_id)
                ->where('express_order_sn', $item->express_order_sn)
                ->select(['product_snapshot_id', 'id', 'issign', 'express_order_sn', 'express_title'])
                ->withCertain('product', ['title', 'image_url'])
                ->get();

            $temp['issign'] = $order_product_list->first()->issign ?? 0;
            $temp['express_order_sn'] = $order_product_list->first()->express_order_sn ?? 0;
            $temp['express_title'] = $order_product_list->first()->express_title ?? 0;
            $temp['id'] = $order_product_list->first()->id ?? 0;;
            $temp['order_product'] = $order_product_list;

            Express::search($item->express_order_sn);

            $list[] = $temp;
        }
        return $this->success([
            'list' => $list
        ]);
    }


    public function info(Request $request)
    {
        $this->validate($request, [
            'order_product_id' => 'required',
        ]);
        $input = $request->input();
        $order_product = MallOrderProduct::with('express')->find($input['order_product_id']);
        if (!$order_product)
            return $this->failed('订单不存在');

        if (!$order_product->express)
            return $this->failed('不存在快递信息');

        $ret['basic'] = $order_product->express;
        $ret['basic']['express_order_sn'] = $order_product->express_order_sn;
        $ret['list'] = Express::search($order_product->express_order_sn);
        return $this->success($ret);
    }


}

